<template>
  <div>
    <el-table :data="articles" style="width: 100%">
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column label="时间" prop="createTime"></el-table-column>
      <el-table-column label="来源" prop="stemfrom"></el-table-column>
      <el-table-column label="阅读" prop="read"></el-table-column>
      <el-table-column label="Star" prop="star"></el-table-column>
      <el-table-column label="评论量" prop="commit"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="update(scope.row)" type="primary" size="mini"
            >编辑</el-button
          >
          <el-button @click="del(scope.row)" type="danger" size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev,pager,next"
      :total="count"
      :page-size="pageSize"
      :current-page="page"
      @current-change="changePage"
      style="margin-top: 20px"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      articles: [],
      page: 1,
      pageSize: 0,
      count: 0,
    };
  },
  methods: {
    getData() {
      this.$http({
        path: "article/find",
        method: "get",
        //注意axios中get的参数传递//🌟
        params: {
          params: {
            page: this.page,
            author: this.$store.state.user.username,
          },
        },
      }).then((res) => {
        this.articles = res.data;
        this.page = res.page;
        this.pageSize = res.pageSize;
        this.count = res.artCount;
      });
    },
    update(row) {
      this.$router.push({
        path:'/admin/article/update',
        query:{
          id:row.id
        }
      })
    },
    del(row) {
      this.$confirm("Are you sure you want to delete it?",
      "Warnning!",{
        confirmButtonText:'删除',
        cancleButtonText:'取消',
        type:"warning"
      }).then(()=>{
        this.$http({
          path:'article/del',
          method:'post',
          params:{
            id:row.id
          }
        }).then(res=>{
          this.$message({
            type:res.code===200?'success':'error',
            message:res.msg
          })
          if(res.code === 200) this.getData()
        })
      })
    },
    changePage(page) {//🌟
      this.page = page;
      this.getData()
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style>
</style>